package com.iscc.fake.controller;

import com.google.gson.Gson;
import com.iscc.fake.domain.Comment;
import com.iscc.fake.domain.Pager;
import com.iscc.fake.domain.Product;
import com.iscc.fake.domain.User;
import com.iscc.fake.service.CommentService;
import com.iscc.fake.service.Impl.CommentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

@WebServlet({"/add_comment","/select_comment","/select_count"})
public class CommentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri =request.getRequestURI();//URI目标为 /fake/register.do；
        String action =uri.substring(uri.lastIndexOf("/")+1);
        PrintWriter out=response.getWriter();
        //转化为Json
        Gson gson =new Gson();
        CommentService commentService=new CommentServiceImpl();
        HashMap<String, Object> commentMap = new HashMap<>();

        //创建session对象
        HttpSession session = request.getSession();
        if ("add_comment".equals(action)){
            //讲存储在seesion中的 User表的 用户编号 拿到
            int uno = ((User) session.getAttribute("user")).getUno();
            //讲存储在seesion中的 product表的 商品编号 拿到
            int pno =Integer.parseInt(request.getParameter("pno"));
            //拿到表中的评价内容
            String ctext = request.getParameter("ctext");
            //拿到表中的评价星级
            int star = Integer.parseInt(request.getParameter("star"));
            Comment comment = new Comment(ctext,uno,star,pno);
            int result = commentService.addComment(comment);

            if (result!=-1){
                commentMap.put("code", 200);
                commentMap.put("msg", "评论成功");
            }else {
                commentMap.put("code", 201);
                commentMap.put("msg", "评论失败");
            }

        }else if ("select_comment".equals(action)){

            int total = commentService.selectCount();
            int currPage=1;
            try {
                currPage=Integer.parseInt(request.getParameter("currPage"));

            }catch (Exception e){
                e.printStackTrace();
            }


            Pager pager = new Pager(currPage,3,total);

            List<Comment> list = commentService.selectComment(pager);



            commentMap.put("code", 200);
            commentMap.put("msg", "查询成功");
            commentMap.put("result",list);
            commentMap.put("pager",pager);

        }
        String str=gson.toJson(commentMap);
        out.print(str);


    }
}
